#include "../Diagnostic/Diagnostic2D.h"
#include "../Field/Field2D.h"
#include "../Parallel/ParallelMPI2D.h"

#include <algorithm>

Diagnostic2D::Diagnostic2D(PicParams* params_, ParallelMPI* pmpi_, Grid* grid_, PhysicalField* fields_, vector<Species*>* vecSpecies_, vector<Collision*>* vecCollision_, vector<PSI*>* vecPSI_) :
    Diagnostic(params_, pmpi_, grid_, fields_, vecSpecies_, vecCollision_, vecPSI_)
{
    Grid2D* grid2D = static_cast<Grid2D*>(grid);

    dx = params->cell_length[0];
    dy = params->cell_length[1];
    dx_inv_   = 1.0/params->cell_length[0];
    dy_inv_   = 1.0/params->cell_length[1];

    ParallelMPI2D* pmpi2D = static_cast<ParallelMPI2D*>(pmpi);
    i_domain_begin = pmpi2D->getCellStartingGlobalIndex(0);
    j_domain_begin = pmpi2D->getCellStartingGlobalIndex(1);

    n_species = params->species_param.size();
    n_line = grid2D->lines.size();
    n_segment2d.resize(n_line);
    n_segment2d_total = 0;
    for(int iLine = 0; iLine < n_line; iLine++)
    {
        n_segment2d[iLine] = grid2D->lines[iLine].size();
        n_segment2d_total += n_segment2d[iLine];
    }


    //init particleFlux
    particleFlux.resize(n_species);
    heatFlux.resize(n_species);
    averageAngle.resize(n_species);
    particleFlux_global.resize(n_species);
    heatFlux_global.resize(n_species);
    averageAngle_global.resize(n_species);
    for (int iSpec = 0 ; iSpec < n_species ; iSpec++) 
    {
        particleFlux[iSpec].resize(n_line);
        heatFlux[iSpec].resize(n_line);
        averageAngle[iSpec].resize(n_line);
        
        particleFlux_global[iSpec].resize(n_line);
        heatFlux_global[iSpec].resize(n_line);
        averageAngle_global[iSpec].resize(n_line);
        for(int iLine = 0; iLine < n_line; iLine++)
        {
            particleFlux[iSpec][iLine].resize(grid2D->lines[iLine].size());
            heatFlux[iSpec][iLine].resize(grid2D->lines[iLine].size());
            averageAngle[iSpec][iLine].resize(grid2D->lines[iLine].size());
            
            particleFlux_global[iSpec][iLine].resize(grid2D->lines[iLine].size());
            heatFlux_global[iSpec][iLine].resize(grid2D->lines[iLine].size());
            averageAngle_global[iSpec][iLine].resize(grid2D->lines[iLine].size());
        }
    }

    
    psiRate.resize(vecPSI->size());
    psiRate_global.resize(vecPSI->size());
    for(int ipsi=0; ipsi<vecPSI->size(); ipsi++)
    {
        psiRate[ipsi].resize(n_line);
        psiRate_global[ipsi].resize(n_line);
        for(int iLine = 0; iLine < n_line; iLine++)
        {
             psiRate[ipsi][iLine].resize(grid2D->lines[iLine].size());
             psiRate_global[ipsi][iLine].resize(grid2D->lines[iLine].size());
        }
    }
    
}

